/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.app.Activity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListAdapter;

import com.mol.market.ui.core.LazyLoadAdapter;

/**
 * 实现的延迟加载的AbsListView<hr>
 * 仅仅实现了延迟加载的逻辑，子类需要自己实现界面已经具体的业务逻辑。
 * @author Chaos
 * @date 2012-12-12
 */
public abstract class LazyLoadAbsListViewFragment extends AbsListViewFragment {
    /**
     * 是否正在加载
     */
    private boolean mIsLoading = false;

    /**
     * 延迟加载的次数
     */
    private int mLazyLoadCount = 0;

    /**
     * 延迟加载用的Adapter
     */
    private LazyLoadAdapter mAdapter = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    /**
     * 这里实现延迟加载
     */
    protected abstract void load();

    /**
     * 开始延迟加载
     */
    public void doLazyLoad() {
        if (mIsLoading) {
            //只允许进行一个任务
            return;
        }
        load();
        if (mLazyLoadCount > 0) {
            showFooterViewByState(getFooterView(), FooterViewState.LOADING);
        }
        mLazyLoadCount++;
    }

    /**
     * 异步加载成功，如果没有更多的数据则移除底部加载视图。
     */
    public void loadSuccess() {
        mIsLoading = false;
        if (!hasMore(mLazyLoadCount)) {
            showFooterViewByState(getFooterView(), FooterViewState.OVER);
        }
    }

    /**
     * 异步加载失败，显示重试提示。
     */
    public void loadFailure() {
        mIsLoading = false;
        mLazyLoadCount--;
        if (mLazyLoadCount == 0) {
            showEmptyViewWhenLoadFail();
        } else {
            showFooterViewByState(getFooterView(), FooterViewState.FAIL);
        }
    }

    /**
     * 得到延迟加载的次数
     */
    public int getLazyLoadCount() {
        return mLazyLoadCount;
    }

    @Override
    protected final ListAdapter initAdapter() {
        mAdapter = initLazyLoadAdapter();
        mAdapter.setActivity(getActivity());
        return mAdapter;
    }

    /**
     * 得到LazyLoadList的Adapter
     * <br>
     * 如果你不确保你的Adapter一定不为空，就必须要做是否为空的检查。
     */
    @Override
    public final LazyLoadAdapter getAdapter() {
        return mAdapter;
    }

    /**
     * 重置LazyLoad的数据
     * @param 
     * @return 
     * @throws
     */
    protected void reset(){
        mIsLoading = false;
        mLazyLoadCount = 0;
    }
    
    @Override
    public void onStart() {
        lzayloadIfNeed();
        super.onStart();
    }

    protected void lzayloadIfNeed(){
        if(getAdapter() == null || getAdapter().getCount() == 0){
            doLazyLoad();
        }
    }
    
    @Override
    protected void initAbsListView(AbsListView absListView) {
        //fix bug by chaos at 2013/3/7
        //横竖屏切换时，如果切换页面正好是FooterView加载页面时，FooterView会消失，
        //避免这个问题的发生就是在构造ListView的时候就让FooterView直接显示为Loading
        showFooterViewByState(getFooterView(), FooterViewState.LOADING);
    }
    
    @Override
    protected void dealloc() {
        mAdapter = null;
        mIsLoading = false;
        mLazyLoadCount = 0;
        super.dealloc();
    }

    protected abstract LazyLoadAdapter initLazyLoadAdapter();

    protected abstract boolean hasMore(int nowLazyLoadCount);

    protected abstract void showEmptyViewWhenLoadFail();

    protected abstract void showFooterViewByState(View FooterView, FooterViewState state);

    public enum FooterViewState {
        /**
         * 正在加载
         */
        LOADING,
        /**
         * 加载失败
         */
        FAIL,
        /**
         * 所有的都加载完毕
         */
        OVER,
        /**
         * 加载成功
         */
        SUCCESS;
    }
}
